package com.bs.visitservice.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bs.visitservice.cache.impl.JedisLoginCache;
import com.bs.visitservice.constant.CacheContant;
import com.bs.visitservice.entity.OrderInfo;
import com.bs.visitservice.entity.Seller;
import com.bs.visitservice.service.SellerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;

@Component
public class OrderInfoQueueDisp implements Runnable{
    private Logger logger = LoggerFactory.getLogger(OrderInfoQueueDisp.class);
    @Autowired
    private SellerService sellerService;
    @Autowired
    private JedisLoginCache jedisLoginCache;
    //订单队列
    public static final LinkedBlockingQueue<OrderInfo> orderInfoQueue = new LinkedBlockingQueue();

    @PostConstruct
    public void init(){
        Thread t = new Thread(this);
        t.start();
    }

    @Override
    public void run() {
        try {
            while(true){
                Thread.sleep(3000);
                OrderInfo orderInfo = orderInfoQueue.take();
                System.out.println(orderInfo);
                Long sellerNo = orderInfo.getSellerNo();
                LambdaQueryWrapper<Seller> wrapper = new LambdaQueryWrapper<>();
                //通过店铺，找到商店登陆编码
                wrapper.eq(Seller::getSellerNo,sellerNo);
                Seller seller = sellerService.getOne(wrapper);
                Long loginNo = seller.getLoginNo();
                //通过登陆编码，查看是否在线
                Object obj = jedisLoginCache.getMapValueCache(CacheContant.SELLER_LOGIN_STATE, String.valueOf(loginNo));
                if(obj == null){
                    logger.error("商家编号["+loginNo+"]档案缓存为空！");
                    //商家编号缓存为空，异常，订单重新加入队列
                    orderInfoQueue.offer(orderInfo);
                    continue;
                }
                Boolean isLogin = (Boolean)obj;

                if(!isLogin){
                    //没有在线订单重新加入队列
                    orderInfoQueue.offer(orderInfo);
                    continue;
                }
                //用户在线
                WebSocketSession session = MessageHandler.LOGIN_NO_SESSIONID.get(loginNo);
                if(session == null){
                    orderInfoQueue.offer(orderInfo);
                    continue;
                }
                try {
                    session.sendMessage(new TextMessage(String.valueOf(orderInfo.getNo())));
                } catch (IOException e) {
                    orderInfoQueue.offer(orderInfo);
                    logger.error("服务端发送订单信息异常！", e);
                    throw new SysException("服务端发送订单信息异常","500");
                }

            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
